package cn.driver.service;

import java.net.URLEncoder;

import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import cn.driver.domain.User;
import cn.driver.mapper.UserMapper;
import cn.driver.utils.MD5Util;
import cn.driver.utils.MyException;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	/**
	 * 登录检查用户名密码
	 * @param id
	 * @param password
	 * @return
	 * @throws MyException 
	 */
	public User login(String username, String password) throws MyException {
		
		User s=userMapper.selectByUsernameAndPassword(username,MD5Util.md5(password));
		if(s==null){
			throw new MyException("用户名或密码错误");
		}else{
			Cookie clogin = new Cookie("userName",URLEncoder.encode(s.getUserName()));
			clogin.setMaxAge(60*60*30);
			clogin.setPath("/");
			return s;
		}
	}
	/**
	 * 注册用户
	 * @param username
	 * @param password
	 */
	public void regist(String username, String password) throws MyException {
		//校验学号是否经被注册
		User s=userMapper.selectUserByUsername(username);
		if(s!=null){
			throw new MyException("该用户已注册！请勿重复注册");
		}
		//MD5加密
		String mdPassword=MD5Util.md5(password);
		userMapper.registUser(username,mdPassword);
	}
	//校验是否绑定
	public void check(String userName) throws MyException {
		String  s=userMapper.check(userName);
		if("0".equals(s)){
			throw new MyException("您还未绑定信息！请绑定！");
		}
		}
	
}
